<?php
session_start();
require "lib/db.php";
require "lib/order-function.php";
require "lib/orderdetail-function.php";
require "lib/product-function.php";
require "lib/customer-function.php";
require "lib/category-function.php";
require "lib/subcategory-function.php";

if(!isset($_SESSION['productNumberTotal'])){
	$_SESSION['productNumberTotal'] = 0;
}

if(!isset($_SESSION['orderAmountTotal'])){
	$_SESSION['orderAmountTotal'] = 0;
}

if(!isset($_SESSION['productIDArray'])){
	$_SESSION['productIDArray'] = array();
}

if(!isset($_SESSION['productNumberArray'])){
	$_SESSION['productNumberArray'] = array();
}


if(isset($_GET['productID'])&& isset($_GET['productNumber'])&& !isset($_GET['action'])){
	$productID = $_GET['productID'];
	settype($productID,"int");
	$product = mysql_fetch_array(getProductByID($productID));
	$price = $product['Price'];
	settype($price,"int");
	$productNumber = $_GET['productNumber'];
	settype($productNumber,"int");
	
	$_SESSION['productNumberTotal'] = $_SESSION['productNumberTotal'] + $productNumber;
	$_SESSION['orderAmountTotal'] =$_SESSION['orderAmountTotal'] + $price*$productNumber;
	
	echo '<td>Product Number Total:<label>  '.$_SESSION['productNumberTotal'].'</label><input name="hfProductNumberTotal" type="hidden" value="'.$_SESSION['productNumberTotal'].'"></td><td>Order Amount Total:<label>  '.$_SESSION['orderAmountTotal'].' VND</label><input name="hfOrderAmountTotal" type="hidden" value="'.$_SESSION['orderAmountTotal'].'"><input id="hfIntelligentSuggestion" name="hfIntelligentSuggestion" type="hidden" value="0"><input id="hfSuggestionCategoryID" name="hfSuggestionCategoryID" type="hidden" value="0"><input id="hfSuggestionSubCategoryID" name="hfSuggestionSubCategoryID" type="hidden" value="0"></td>';
	$_SESSION['productNumberTotal'] = $_SESSION['productNumberTotal'] - $productNumber;
	$_SESSION['orderAmountTotal'] =$_SESSION['orderAmountTotal'] - $price*$productNumber;
}

//add more one product to order
if(isset($_GET['productID'])&& isset($_GET['productNumber'])&& isset($_GET['action'])){
	$intelligentSuggestion = $_GET['intelligentSuggestion'];
	$productID = $_GET['productID'];
	settype($productID,"int");
	$product = mysql_fetch_array(getProductByID($productID));
	$price = $product['Price'];
	settype($price,"int");
	$productNumber = $_GET['productNumber'];
	settype($productNumber,"int");
	
	$_SESSION['productNumberTotal'] = $_SESSION['productNumberTotal'] + $productNumber;
	$_SESSION['orderAmountTotal'] =$_SESSION['orderAmountTotal'] + $price*$productNumber;
	
	//check product already in cart or not
	$check = false;
	$id;
	for($i=0;$i<count($_SESSION['productIDArray']);$i++){
		if($_SESSION['productIDArray'][$i] == $productID){
			$check = true;
			$id = $i;
			break;
		}
	}
	if($check==true){//product in cart
		$_SESSION['productNumberArray'][$id] = $_SESSION['productNumberArray'][$id] + $productNumber;
	}else{//not in cart
	array_push($_SESSION['productIDArray'],$productID);
	array_push($_SESSION['productNumberArray'],$productNumber);
	}
	
	$suggestionProductID = 0;
	$suggestionCategoryID = 0;
	$suggestionSubCategoryID = 0;
	if($intelligentSuggestion == "checked" && getConsequenceByAntecedence($productID)!=null){
		$suggestionProductID = getConsequenceByAntecedence($productID);
		$suggestionCategory = mysql_fetch_array(getCategoryByProductID($suggestionProductID));// wrong $productID, right: $suggestionProductID
		$suggestionCategoryID = $suggestionCategory['CategoryID'];
		$suggestionSubCategory = mysql_fetch_array(getSubCategoryByProductID($suggestionProductID));// wrong $productID, right: $suggestionProductID
		$suggestionSubCategoryID = $suggestionSubCategory['SubCategoryID'];
	}
	
	echo '<td>Product Number Total:<label>  '.$_SESSION['productNumberTotal'].'</label><input name="hfProductNumberTotal" type="hidden" value="'.$_SESSION['productNumberTotal'].'"></td><td>Order Amount Total:<label>  '.$_SESSION['orderAmountTotal'].' VND</label><input name="hfOrderAmountTotal" type="hidden" value="'.$_SESSION['orderAmountTotal'].'"><input id="hfIntelligentSuggestion" name="hfIntelligentSuggestion" type="hidden" value="'.$suggestionProductID.'"><input id="hfSuggestionCategoryID" name="hfSuggestionCategoryID" type="hidden" value="'.$suggestionCategoryID.'"><input id="hfSuggestionSubCategoryID" name="hfSuggestionSubCategoryID" type="hidden" value="'.$suggestionSubCategoryID.'"></td>';
}

if(isset($_POST['btnAddOrder'])){
	$customerID = $_POST['cbCustomer'];
	settype($customerID,"int");
	$orderYear = $_POST['cbOrderYear'];
	settype($orderYear,"int");
	$orderMonth = $_POST['cbOrderMonth'];
	settype($orderMonth,"int");
	$productID = $_POST['cbProductName'];
	settype($productID,"int");
	$productNumber = $_POST['txtProductNumber'];
	settype($productNumber,"int");
	
	if($productID > 0 && $productNumber > 0){
		$product = mysql_fetch_array(getProductByID($productID));
		$price = $product['Price'];
		settype($price,"int");
		
		$_SESSION['productNumberTotal'] = $_SESSION['productNumberTotal'] + $productNumber;
		$_SESSION['orderAmountTotal'] =$_SESSION['orderAmountTotal'] + $price*$productNumber;
		
		//check product already in cart or not
		$check = false;
		$id;
		for($i=0;$i<count($_SESSION['productIDArray']);$i++){
			if($_SESSION['productIDArray'][$i] == $productID){
				$check = true;
				$id = $i;
				break;
			}
		}
		if($check==true){//product in cart
			$_SESSION['productNumberArray'][$id] = $_SESSION['productNumberArray'][$id] + $productNumber;
		}else{//not in cart
		array_push($_SESSION['productIDArray'],$productID);
		array_push($_SESSION['productNumberArray'],$productNumber);
		}
	}
	
	$productNumberTotal = $_SESSION['productNumberTotal'];
	$orderAmountTotal = $_SESSION['orderAmountTotal'];
	
	//add to customer amount total
	$customer = mysql_fetch_array(getCustomerByID($customerID));
	$customerAmountTotal = $customer['CustomerAmountTotal'];
	settype($customerAmountTotal,"int");
	$customerTypeID = $customer['CustomerTypeID'];
	settype($customerTypeID,"int");
	$customerAmountTotal = $customerAmountTotal + $orderAmountTotal;
	if($customerAmountTotal > 500000 && $customerAmountTotal < 1000000){
		$customerTypeID = 2;
	}
	if($customerAmountTotal > 1000000 && $customerAmountTotal < 5000000){
		$customerTypeID = 3;
	}
	if($customerAmountTotal > 5000000){
		$customerTypeID = 4;
	}
	editCustomerAmountTotalAndCustomerType($customerAmountTotal,$customerTypeID,$customerID);
	
	//add order
	$orderID = addOrder($customerID,$orderMonth,$orderYear,$productNumberTotal,$orderAmountTotal);
	//add orderdetail of order
	for($i = 0; $i < count($_SESSION['productIDArray']);$i++){//$i should begin is 0 because in array begin with index 0
		addOrderDetail($orderID,$_SESSION['productIDArray'][$i],$_SESSION['productNumberArray'][$i]);
		//add to product amount total
		$productID = $_SESSION['productIDArray'][$i];
		$product = mysql_fetch_array(getProductByID($productID));
		$price = $product['Price'];
		settype($price,"int");
		$productNumber = $_SESSION['productNumberArray'][$i];
		$productAmountTotal = $price*$productNumber + $product['ProductAmountTotal'];//missing + $product['ProductAmountTotal']
		editProductAmountTotal($productAmountTotal,$productID);
		
		//add to subcategory amount total
		$subcategory = mysql_fetch_array(getSubCategoryByProductID($productID));
		$subcategoryAmountTotal = $subcategory['SubCategoryAmountTotal'];
		settype($subcategoryAmountTotal,"int");
		$subcategoryAmountTotal = $subcategoryAmountTotal + $price*$productNumber;//missing $price*$productNumber, wrong with $productAmountTotal
		editSubCategoryAmountTotal($subcategoryAmountTotal,$subcategory['SubCategoryID']);
		
		//add to category amount total
		$category = mysql_fetch_array(getCategoryByProductID($productID));
		$categoryAmountTotal = $category['CategoryAmountTotal'];
		settype($categoryAmountTotal,"int");
		$categoryAmountTotal = $categoryAmountTotal +$price*$productNumber;//missing $price*$productNumber, wrong with $productAmountTotal
		editCategoryAmountTotal($categoryAmountTotal,$category['CategoryID']);
	}
	
	//after add order set these array and value store data to 0
	$_SESSION['productNumberTotal'] = 0;
	$_SESSION['orderAmountTotal'] = 0;
	$_SESSION['productIDArray'] = array();
	echo count($_SESSION['productIDArray']);
	$_SESSION['productNumberArray'] = array();
	header("location:list-order.php");
}

function getConsequenceByAntecedence($productID){
	$qr="select * from `association-rule` where Antecedence = $productID";
	$rule = mysql_fetch_array(mysql_query($qr));
	return $rule['Consequence'];
}

?>